const express=require("express");
const userLogin = require("../server/userLogin");
const autuorslist = require("../server/usertitle")
const authorContent = require("../server/usercontent")
const delArticle = require("../server/delArticle")
const addArticle = require("../server/addArticle")
const classifyArticleList = require("../server/classifyList")
const router=express.Router();
//登录接口
router.post("/userlogin",(req,res)=>{
    let params=req.body
    let username=params.userName
    let userpwd=params.userpassword
    res.end("登录成功")
    userLogin(username,userpwd)
})
//首页推荐作者接口
router.get("/authorsList",(req,res)=>{
    autuorslist().then(data =>{
        if(data==4004){
            res.json({
                status:data,
                msg:"作者列表为空"
            })
            res.end()
        }else{
            res.json({
                status:200,
                msg:data
            })
        }
        res.end()
    })
})
//文章详情接口
router.get("/articleList",(req,res)=>{
    authorContent().then(data=>{
        if(data == 4004){
            res.json({
                status:data,
                msg:"内容为空"
            })
            res.end()

        }else{
            res.json({
                status:200,
                msg:data
            })
        }
        res.end()
    })
})


//删除接口
router.get("/deleteArticle",(req,res)=>{
    let params=req.body;
    let ids=params._id;
    console.log(params)
    delArticle(ids).then(data=>{
        if(data==500){
            res.json({
                status : data,
                msg :"文章删除失败"  
            })
            res.end()
        }else if(data==200){
            res.json({
                status : data,
                msg:"ture"
            })
            res.end()
        }
    })

})

//新增接口
router.post("/addArticle",(req,res)=>{
    let params = req.body;
    let art_id = params._id;
    let art_title = params.title;
    let art_content = params.content;
    let art_author = params.author;
    let art_authorId = params.authorId;
    addArticle(art_id,art_title,art_content,art_author,art_authorId)
    if(data==500){
        res.json({
            status : data,
            msg :"添加文章失败"  
        })
        res.send()
    }else if(data==200){
        res.json({
            status : data,
            msg:"ture"
        })
        res.send()
    }
})

//更新接口
router.post("/updateIdArticle",(req,res)=>{
    let params=req.body;
    let up_id=params._id;
    let up_title=params.title;
    let up_content=params.content;
    updateIdArticle(up_id,up_title,up_content).then(data=>{
        if(data==500){
            res.json({
                status : data,
                msg :"更改文章失败"  
            })
            res.send()
        }else if(data==200){
            res.json({
                status : data,
                msg:"ture"
            })
            res.send()
        }
    })
})

//注册接口
router.post("/addUser",(req,res)=>{
    let params=req.body;
    let userId=params._id;
    let userName=params.userName;
    let userPassword=params.userpassword;
    let headImg=params.headImg;
    console.log(params)
    addUser(userId,userName,userPassword,headImg).then(data=>{
        if(data==500){
            res.json({
                status : data,
                msg :"注册失败请重新注册"  
            })
            res.send()
        }else if(data==200){
            res.json({
                status : data,
                msg:"注册成功"
            })
            res.send()
        }
    })
})

//文章分类页加载文章列表数据
router.get("/classifyArticleList",(req,res)=>{
    let params=req.body;
    let article_classify=params.article_classify;
    classifyArticleList(article_classify).then(data=>{
        if(data==404){
            res.json({
                status:data,
                msg:"作者列表为空"
            })
           res.end()
        }else{
            res.json({
                status:200,
                msg:data
            })
            res.end()
    
        }
    })
})

module.exports=router;